<template>
  <!-- element级联组件 -->
  <el-cascader :value="value" size="mini" :options="treeData" :props="props" separator="-" @change="changeValue" />
</template>
<script>
import { getDepartment } from '@/api/department'
import { transListToTreeData } from '@/utils/index.js'
export default {
  props: {
    value: {
      type: Number, // 存储部门id
      default: null
    }
  },
  data() {
    return {
      treeData: [], // 赋值给级联组件的options属性值
      props: {
        label: 'name', // 要显示的字段名
        value: 'id'// 要存储的字段
      }
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    async getDepartment() {
      const result = await getDepartment()
      const data = transListToTreeData(result, 0)
      this.treeData = data
    },
    changeValue(list) {
      if (list.length > 0) {
        this.$emit('input', list[list.length - 1])// 将最后一位的id传给父组件进行存储
      } else {
        this.$emit('input', null)
      }
    }
  }
}
</script>
